package com.wish.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.api.SysLogApi;
import com.wish.dto.MenuDto;
import com.wish.entity.SysMenu;
import com.wish.service.ISysMenuService;
import com.wish.vo.ResultEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author 五组
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/wish/sys-menu")
public class SysMenuController {
    @Resource
    private ISysMenuService iSysMenuService;

    @RequestMapping("/list")
    public ResultEntity list(MenuDto menuDto){
        //PageHelper.startPage(menuDto.getPageNum(),menuDto.getPageSize());
        List<SysMenu> list = iSysMenuService.selects();
        if(!list.isEmpty()){
            for (SysMenu sysMenu:list) {
                List<SysMenu> sons = getSons(sysMenu.getMenuId());
                sysMenu.setSons(sons);
            }
        }
        return ResultEntity.ok("200","查询成功",list);
    }

    @RequestMapping("/getMenus")
    public ResultEntity getMenus(){
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("type",0);
        List<SysMenu> list = iSysMenuService.list(wrapper);
        if(!list.isEmpty()){
            for (SysMenu sysMenu:list) {
                List<SysMenu> sons = getSons(sysMenu.getMenuId());
                sysMenu.setSons(sons);
            }
        }
        return ResultEntity.ok(list);
    }

    public List<SysMenu> getSons(Long id){
        /*QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);*/
        List<SysMenu> list = iSysMenuService.getList(id);
        if(list==null){
            return list;
        }else {
            for (SysMenu sysMenu:list) {
                List<SysMenu> sons = getSons(sysMenu.getMenuId());
                sysMenu.setSons(sons);
            }
            return list;
        }
    }
    @SysLogApi("添加菜单")
    @RequestMapping("add")
    public boolean add(@RequestBody SysMenu sysMenu){
        return iSysMenuService.save(sysMenu);
    }
    @SysLogApi("修改菜单")
    @RequestMapping("update")
    public boolean update(@RequestBody SysMenu sysMenu){
        return iSysMenuService.updateById(sysMenu);
    }

    @SysLogApi("删除菜单")
    @RequestMapping("delById")
    public boolean delById(Long[] ids){
        try {
            for (Long id:ids) {
                iSysMenuService.removeById(id);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("getOne")
    public List<SysMenu> getOne(Long id){
        return iSysMenuService.getOne(id);
    }

    @RequestMapping("/getMenuss")
    public ResultEntity getMenus(String username) {
        List<SysMenu> menuList = iSysMenuService.getMenus(username);
        return ResultEntity.ok("200","查询菜单成功",menuList);
    }

}
